{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 礼帽与黑帽"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 礼帽"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 礼帽 = 原始输入-开运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T03:14:06.872978800Z",
     "start_time": "2024-04-26T03:14:05.307941900Z"
    }
   },
   "outputs": [],
   "source": [
    "import cv2 #opencv的缩写为cv2\n",
    "import matplotlib.pyplot as plt # matplotlib库用于绘图展示\n",
    "import numpy as np   # numpy数值计算工具包\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook特有\n",
    "%matplotlib inline   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T03:14:10.744698700Z",
     "start_time": "2024-04-26T03:14:06.884984800Z"
    }
   },
   "outputs": [],
   "source": [
    "# 礼帽 \n",
    "# 原始带刺，开运算不带刺，原始输入-开运算 = 刺\n",
    "img = cv2.imread('01_Picture/05_Dige.png')\n",
    "kernel = np.ones((5,5),np.uint8)\n",
    "tophat = cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)\n",
    "cv2.imshow('tophat',tophat)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 黑帽"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "② 黑帽 = 闭运算-原始输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T03:15:21.821072500Z",
     "start_time": "2024-04-26T03:14:51.125599800Z"
    }
   },
   "outputs": [],
   "source": [
    "# 黑帽  \n",
    "# 原始带刺，闭运算带刺并且比原始边界胖一点，闭运算-原始输入 = 原始整体\n",
    "img = img = cv2.imread('01_Picture/05_Dige.png')\n",
    "kernel = np.ones((2,2),np.uint8)\n",
    "blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)\n",
    "cv2.imshow('blackhat',blackhat)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
